<?php

class PropertyAppraisalsController extends AppController {

    var $name = 'PropertyAppraisals';

    function edit($id) {
        /*     $this->set('cod', $this->Session->read('cod'));
          $this->set('proy_id', $this->Session->read('proy_id'));

          $this->Appraisal->recursive = -1; */

        if (empty($this->data)) {
            //devuelve los datos que hay en la base de datos
            $this->data = $this->PropertyAppraisal->find("first", array("conditions" => array("PropertyAppraisal.id" => $id)));
        } else {
            //guarda los datos modificados
            if ($this->PropertyAppraisal->save($this->data)) {
                $this->Session->setFlash('Información del avaluo actualizada con exito');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
            } else {
                //si no fueron guardados 
                $this->Session->setFlash('Error guardando datos');
            }
        }
    }

    function add_catastral($id) {
        if (empty($this->data)) {
            $this->data = $this->PropertyAppraisal->find("first", array("conditions" => array("PropertyAppraisal.id" => $id),'fields'=>array('Property.np_departamento','Appraisal.bloqueo','Property.np_municipio','Property.np_tipo','Property.np_sector','Property.np_vereda','Property.np_predio','Property.np_mejora','Property.id','PropertyAppraisal.avaluo_vigente','PropertyAppraisal.vigencia_actualizacion','PropertyAppraisal.observaciones','PropertyAppraisal.observaciones','PropertyAppraisal.id','PropertyAppraisal.property_id')));
        } else {
            $idap=$this->data['PropertyAppraisal']['id'];
            $idpred=$this->data['PropertyAppraisal']['property_id'];
            if ($this->PropertyAppraisal->saveAll($this->data)) {
                $this->Session->setFlash('Información del avaluo actualizada con exito');
                $this->redirect(array('controller' => 'PropertyAppraisals', 'action' => 'catastral_index'));
            }else{
                // $this->data = $this->PropertyAppraisal->find("first", array("conditions" => array("PropertyAppraisal.id" => $id)));
                $this->data['PropertyAppraisal']['id']= $idap;
                $this->data['PropertyAppraisal']['property_id']= $idpred;
            }
        }
    }

    function area_index() {
        $id = $this->Session->read('proy_id');
        $propiedades = $this->PropertyAppraisal->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('PropertyAppraisal.id', 'Property.codigo', 'Property.nombre', 'Property.id', 'PropertyAppraisal.area_levantamiento')));
        $this->set("propiedades", $propiedades);
    }

    function catastral_index() {
        $id = $this->Session->read('proy_id');
        $idap = $this->PropertyAppraisal->Appraisal->field('id', array('Appraisal.proyect_id' => $id));
        $propiedades = $this->PropertyAppraisal->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('PropertyAppraisal.property_id', 'PropertyAppraisal.appraisal_id', 'PropertyAppraisal.id', 'Property.codigo', 'Property.nombre', 'Property.id', 'PropertyAppraisal.vigencia_actualizacion', 'PropertyAppraisal.avaluo_vigente','PropertyAppraisal.observaciones')));
        $this->set("idap", $idap);
        $this->set("propiedades", $propiedades);
    }

    function constructions_index() {
        $id = $this->Session->read('proy_id');
        $propiedades = $this->PropertyAppraisal->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('PropertyAppraisal.id', 'Property.codigo', 'Property.nombre', 'Property.id')));
        $this->set("propiedades", $propiedades);
    }

    function constructions_valor_index() {
        $id = $this->Session->read('proy_id');
        $propiedades = $this->PropertyAppraisal->Property->Construction->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Construction.id', 'Property.codigo', 'Construction.nombre', 'Construction.valor_adoptado', 'Construction.area', 'Construction.unidad_de_medida')));
        $this->set("propiedades", $propiedades);
    }

    function annexes_valor_index() {
        $id = $this->Session->read('proy_id');
        $anexos = $this->PropertyAppraisal->Property->Annex->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Annex.id', 'Property.codigo', 'Annex.nombre', 'Annex.valor_adoptado', 'Annex.area', 'Annex.unidad_de_medida')));
        $this->set("anexos", $anexos);
    }

    function add_legal_information($id) {
        if (empty($this->data)) {
            $this->data = $this->PropertyAppraisal->find("first", array("conditions" => array("PropertyAppraisal.id" => $id), 'fields' => array('PropertyAppraisal.titulo', 'PropertyAppraisal.id', 'PropertyAppraisal.matricula', 'PropertyAppraisal.propietarios', 'PropertyAppraisal.servidumbre','PropertyAppraisal.property_id','PropertyAppraisal.appraisal_id')));
            if ($this->data['PropertyAppraisal']['matricula'] == "") {
                $matricula=$this->PropertyAppraisal->Property->field('matricula',array('Property.id'=>$this->data['PropertyAppraisal']['property_id']));
                $this->data['PropertyAppraisal']['matricula']=$matricula;
            }
            if($this->data['PropertyAppraisal']['propietarios']==""){
                $nombres="";
                $propietarios=$this->PropertyAppraisal->Property->Owner->find('all',array('conditions'=>array('Property.id'=>$this->data['PropertyAppraisal']['property_id']),'fields'=>array('Owner.nombres','Owner.segundo_apellido','Owner.primer_apellido')));
                foreach ($propietarios as $prop) {
                    $nombres.="\n".$prop['Owner']['nombres']." ".$prop['Owner']['primer_apellido']." ".$prop['Owner']['segundo_apellido'];
                }
                $this->data['PropertyAppraisal']['propietarios']=$nombres;
            }
            
        } else {
            if ($this->PropertyAppraisal->save($this->data)) {
                $this->Session->setFlash('Información del avaluo actualizada con exito');
                $this->redirect(array('controller' => 'PropertyAppraisals', 'action' => 'legal_information'));
            }
        }
    }

    function legal_information() {
        App::Import('model', 'Owner');
        $p = new Owner();

        $id = $this->Session->read('proy_id');
        $propiedades = $this->PropertyAppraisal->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('PropertyAppraisal.appraisal_id', 'PropertyAppraisal.id', 'PropertyAppraisal.property_id', 'Property.codigo', 'Property.nombre', 'Property.id', 'Property.matricula', 'PropertyAppraisal.titulo', 'PropertyAppraisal.servidumbre')));
        $propietarios = $p->find('list', array('conditions' => array('Owner.property_id' => $this->data['Property']['id']), 'fields' => array('Owner.nombres', 'Owner.primer_apellido', 'Owner.segundo_apellido')));
        $this->set("propiedades", $propiedades);
        $this->set("propietarios", $propietarios);
    }

    function add_area($id) {
        if (empty($this->data)) {
            $this->data = $this->PropertyAppraisal->find("first", array("conditions" => array("PropertyAppraisal.id" => $id), 'fields' => array('PropertyAppraisal.id', 'PropertyAppraisal.area_levantamiento')));
        } else {
            if ($this->PropertyAppraisal->save($this->data)) {
                $this->Session->setFlash('Información del avaluo actualizada con exito');
                $this->redirect(array('controller' => 'PropertyAppraisals', 'action' => 'area_index'));
            }
        }
    }

    function delete($id, $pr_id) {
        if ($this->PropertyAppraisal->delete($id)) {
            if ($this->PropertyAppraisal->Property->Annex->deleteAll(array('Annex.property_id' => $pr_id), false)) {
                if ($this->PropertyAppraisal->Property->Construction->deleteAll(array('Construction.property_id' => $pr_id), false)) {

                    $this->Session->setFlash('Registro Eliminado con exito');
                    $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
                }
            }
        }
    }

    function update_properties($appraisal_id) {
        $id = $this->Session->read('proy_id');
        $this->PropertyAppraisal->Property->recursive = -1;
        $predios = $this->PropertyAppraisal->Property->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id')));

        foreach ($predios as $predio) {
            if ($this->PropertyAppraisal->find('count', array('conditions' => array('PropertyAppraisal.property_id' => $predio['Property']['id']))) == 0) {
                $this->PropertyAppraisal->create();
                $array = array(
                    'appraisal_id' => $appraisal_id,
                    'property_id' => $predio['Property']['id']
                );

                $this->PropertyAppraisal->save($array);
            }
        }
        $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
    }

}

?>
